import React from 'react';
import {Router, Route,Link,hashHistory,IndexRoute, Redirect} from 'react-router';

import Menus from './Menus';
require('es6-promise').polyfill();
require('isomorphic-fetch');


export default class AddRess extends React.Component{
    constructor(props) {
        super(props);
        this.state={
            addRessList:[],
            userid:localStorage.getItem('userid')
        }
    }
    componentWillMount(){
        if(this.state.userid==null || this.state.userid==''){
            hashHistory.push({
                pathname:"/login"
            })
        }else {
            this.getAddressListss();
        }

    }
    //点击地址设置默认地址
    setDefaultAddress(index){
        let userAddress=this.state.addRessList[index];
        localStorage.setItem("userAddress",userAddress);
        hashHistory.push({
            pathname:"/orderHouseIndex/jinpai/order"
        })
    }
    getAddressListss(){
        /*  body:JSON.stringify({'userid':userid}),*/
        fetch('text/addRess.json',
            {
                method: 'get',
                headers: {
                    'Accept': 'application/json',
                    "Content-Type": "application/json"
                },

            }
        ).then(function(response) {
            return response.json();
        }).then((json)=>{
           this.setState({
               addRessList:json.address,
           })
        }).catch(function(ex) {
            console.log('parsing failed', ex)
        })
    }

    render(){
        return(
            <div style={{paddingTop:'15px',height:'100%',backgroundColor:'#efeff4'}}>
               <ul className="address-list">
                   {
                       this.state.addRessList.map((item,index)=>{
                           return(
                              <li key={index} onClick={this.setDefaultAddress.bind(this,index)}>
                                  {item}
                              </li>
                           )
                       })
                   }
               </ul>
                <Link to="/inputAddress">
                    <div className="all-button9">添加一个地址</div>
                </Link>
                <Menus/>
            </div>
        )
    }
}
